<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CommentController
 *
 * @author tuancasi
 */
class CommentController extends Controller {

    /**
     * add comment for 1 post
     */
    public function actionAdd() {
	if (isset($_POST['post_id']) && isset($_POST['comment'])) {
	    //get list bad word
	    $aBadWord = FilterWord::getListFilter();

	    $modelComment = new Comment();
	    $modelComment->content = $_POST['comment'];
	    $modelComment->post_id = $_POST['post_id'];
	    $modelComment->user_id = user()->getModel()->user_id;
	    $modelComment->time_created = date('Y-m-d H:i:s', time());
	    $modelComment->save();

	    echo '<div class="box-li">';
	    echo '<div class="left-li">';
	    echo '<img src="' . user()->getModel()->getAvatarUrl() . '" />';
	    echo '<p><a>' . h(user()->getModel()->username, 9) . '</a></p>';
	    echo '</div>';
	    echo '<div class="right-li">';
	    echo '<div class="mui"></div>';
	    echo '<div class="date-com">';
	    echo '<table><tr>';
	    echo '<td>' . date('m-d-Y', strtotime($modelComment->time_created)) . '</td>';
	    echo '<td></td>';
	    echo '</tr></table>';
	    echo '</div>';
	    echo '<div class="text-li">';
	    echo nl2br(h($modelComment->content, 0, FilterWord::getListFilter()));
	    echo '</div>';
	    echo '</div>';
	    echo '</div>';
	}
    }

    /**
     * get more comments
     */
    public function actionGet() {
	if (isset($_POST['nextPage']) && isset($_POST['idPost'])) {
	    $currPage = ($_POST['nextPage'] - 1) * Comment::TOTAL_RECORD;
	    $modelComment = Comment::model()->with('user')->findAll(
		    array(
			'condition' => 't.post_id=:post_id',
			'params' => array(':post_id' => $_POST['idPost']),
			'order' => 't.comment_id DESC',
			"limit" => Comment::TOTAL_RECORD,
			"offset" => $currPage
		    )
	    );

	    foreach ($modelComment as $comment) {
		echo '<div class="box-li">';
		echo '<div class="left-li">';
		echo '<img src="' . $comment->user->getAvatarUrl() . '" />';
		echo '<p><a href = "' . SITE_NAME_URL . 'user/' . $comment->user_id . '">' . h($comment->user->username, 9) . '</a></p>';
		echo '</div>';
		echo '<div class="right-li">';
		echo '<div class="mui"></div>';
		echo '<div class="date-com">';
		echo '<table><tr>';
		echo '<td>' . date('m-d-Y', strtotime($comment->time_created)) . '</td>';
		echo '<td>';
		$this->widget('application.widgets.reportabuse.ReportAbuse', array('model' => $comment));
		echo '</td>';
		echo '</tr></table>';
		echo '</div>';
		echo '<div class="text-li">';
		echo nl2br(h($comment->content, 0, FilterWord::getListFilter()));
		echo '</div>';
		echo '</div>';
		echo '</div>';
	    }
	}
    }

}
